﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DJY.Code.Helper
{
    public static class WorkFlowHelper
    {
        /// <summary>
        /// 流程节点处理结果
        /// </summary>
        public const string NODE_RESULT_KEY = "ProcessResult";

        /// <summary>
        /// 流程节点成功处理结果
        /// </summary>
        public const string NODE_OK_RESULT = "1";

        /// <summary>
        /// 流程节点失败处理结果
        /// </summary>
        public const string NODE_FAIL_RESULT = "0";

        /// <summary>
        /// 流程节点审核结果
        /// </summary>
        public const string NODE_AUDIT_KEY = "AuditResult";

        /// <summary>
        /// 流程节点审核说明
        /// </summary>
        public const string NODE_AUDIT_REMARK = "AuditRemark";

        /// <summary>
        /// 流程节点成功审核结果
        /// </summary>
        public const string NODE_OK_AUDIT = "1";

        /// <summary>
        /// 流程节点失败审核结果
        /// </summary>
        public const string NODE_FAIL_AUDIT = "0";

        /// <summary>
        /// 页面键设置
        /// </summary>
        public const string MODEL_KEYS_KEY = "modelKeys";

        //忽略空值
        public static readonly JsonSerializerSettings IgnoreNullSettings = new JsonSerializerSettings() {
            NullValueHandling = NullValueHandling.Ignore,
            Formatting = Formatting.None
        };

        /// <summary>
        /// 解析模型类
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="error">错误信息</param>
        /// <returns></returns>
        public static Type GetModelType(string type, out string error)
        {
            if (string.IsNullOrEmpty(type))
            {
                throw new ArgumentNullException(nameof(type));
            }

            string key = type.Trim().Replace(" ", "");
            if (key.IndexOf('.') == -1)
            {
                key = "DJY.Domain.ViewModel." + key;
            }

            if (key.IndexOf(",") == -1)
            {
                key += ",DJY.Domain";
            }

            Type result = null;
            error = null;
            try
            {
                result = Type.GetType(key);
                if (result == null)
                {
                    error = $"类型{type}未找到";
                }
            }
            catch
            {
                error = $"类型{type}未找到";
            }

            return result;
        }

        /// <summary>
        /// 解析事件方法
        /// </summary>
        /// <param name="eventName">类型</param>
        /// <param name="args">参数</param>
        /// <param name="error">错误信息</param>
        /// <returns></returns>
        public static MethodInfo GetEventMethod(string eventName, Type[] args, out string error)
        {
            if (string.IsNullOrEmpty(eventName))
            {
                throw new ArgumentNullException(nameof(eventName));
            }

            string key = eventName.Trim().Replace(" ", "");
            if (key.IndexOf('.') == -1)
            {
                //[WorkFlowEngine.]StaticMethod
                key = "DJY.Application.WorkFlow.WorkFlowEngine." + key;
            }

            if (key.IndexOf(",") == -1)
            {
                //Class.StaticMethod
                if (key.IndexOf('.') == key.LastIndexOf('.'))
                {
                    key = "DJY.Application." + key;
                }

                key += ",DJY.Application";
            }

            error = null;

            MethodInfo result = null;
            string[] keys = key.Split(',');
            int idx = keys[0].LastIndexOf(".");
            string method = keys[0].Substring(idx + 1);
            keys[0] = keys[0].Substring(0, idx);
            try
            {
                Type type = Type.GetType(string.Join(",", keys));
                if (type == null)
                {
                    error = $"类型({eventName})未找到";
                }

                result = type.GetMethod(method, args);
                if (result == null)
                {
                    error = $"方法({method})未找到";
                }

                if (!result.IsStatic)
                {
                    error = $"方法({method})不是静态";
                }
            }
            catch
            {
                error = $"类型({eventName})未找到";
            }

            return result;
        }
    }
}
